home *** CD-ROM | disk | FTP | other *** search
/ Aminet 16 / Aminet 16 (1996)(GTI - Schatztruhe)[!][Dec 1996].iso / Aminet / misc / emu / Easy1541.lha / Easy1541 / dev / Examples / IECLoad.c < prev    next >
C/C++ Source or Header  |  1996-09-02  |  2KB  |  141 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <exec/exec.h>
  5. #include <exec/execbase.h>
  6. #include <exec/memory.h>
  7. #include <dos/dos.h>
  8. #include <proto/exec.h>
  9. #include <proto/dos.h>
  10.  
  11. #include <iec/iec.h>
  12.  
  13. //----------------------------------------
  14. //IECLoad 1.0
  15. //
  16. //Reads a file from the disk inserted in
  17. //the 1541 drive, and writes it to AmigaDos.
  18. //
  19. //----------------------------------------
  20.  
  21.  
  22. struct iecbase *IECBase;
  23.  
  24. char Version[]="$VER:IECLoad 1.0 (01.09.96) by Fabrizio Farenga";
  25. UWORD BlockSize;
  26. unsigned char c;
  27. char* SourceName;    //File to read (from 1541)
  28. char* DestName;        //File to write (to AmigaDos)
  29.  
  30.  
  31. FILE *fp;    //Output file
  32.  
  33. struct RDArgs *rda;
  34. LONG argv[3]={0,0,0};
  35. char device=8;    //Default device #
  36.  
  37. //******************************************
  38. // Send a null-terminated string to the 1541
  39. //******************************************
  40. void SendString(char* string)
  41. {
  42. int t;
  43. for (t=0;string[t]!=0;t++) CIOut(string[t]); 
  44. }
  45.  
  46. void ReadFile(void)
  47. {
  48.  
  49. fp=fopen(DestName,"wb");    //Open file to write (AmigaDos)
  50.  
  51.     if (fp!=0)
  52.     {
  53.     printf ("\n");
  54.  
  55.  
  56.     //LOAD "<SourceName>",<device>
  57.     Listen(device);
  58.     Second(CMD_OPEN+0);
  59.  
  60.  
  61.     if (IECBase->iec_ST!=ST_OK)
  62.         {
  63.         printf ("?DEVICE NOT PRESENT\n\n");
  64.         return;
  65.         }
  66.  
  67.  
  68.     //Send the filename
  69.     SendString(SourceName);
  70.     UnListen();
  71.  
  72.     //Receive data
  73.     Talk(device);
  74.     TkSA(CMD_DATA+0);
  75.  
  76.         while (IECBase->iec_ST==ST_OK)
  77.         {
  78.         c=ACPtr();                    //Read a byte from 1541
  79.             if (IECBase->iec_ST==ST_READ_TIMEOUT)    //On file not found break.
  80.             {
  81.             printf ("?FILE NOT FOUND ERROR\n\n");
  82.             break;
  83.             }
  84.         if (EOF==putc(c,fp)) break;    //Write the byte to AmigaDos
  85.         if (IECBase->iec_ST!=ST_OK) break;    //On error or EOF break
  86.         }
  87.  
  88.     UnTalk();
  89.  
  90.     Listen(device);    //CLOSE
  91.     Second(CMD_CLOSE+0);
  92.     UnListen();
  93.  
  94.     fclose(fp);
  95.     }
  96. }
  97.  
  98.  
  99. void main(void)
  100. {
  101.  
  102. if ((rda = ReadArgs("FROM/A,TO,DEVICE/N",argv,NULL)) != NULL)
  103.     {
  104.     if (argv[0]!=0) SourceName=(char*)argv[0];    //Source filename
  105.  
  106.     if (argv[1]!=0) DestName=(char *)argv[1];    //Destination filename
  107.     else DestName=(char *)argv[0];                //If none, use the source name
  108.  
  109.     if (argv[2]!=0) device=*(LONG *)argv[2];    //Device (if none, device = 8)
  110.     }
  111. else
  112.     {
  113.     printf ("Required argument missing\n\n");
  114.     return;
  115.     }
  116.  
  117.  
  118. if (device<4)
  119.     {
  120.     printf ("\n?DEVICE NOT PRESENT\n\n");
  121.     return;
  122.     }
  123.  
  124. IECBase = (struct iecbase*)OpenLibrary("iec.library",0L);
  125.  
  126. if (IECBase!=0)
  127.     {
  128.  
  129.     ReadFile();
  130.  
  131.     CloseLibrary((struct Library*)IECBase);
  132.     }
  133. else
  134.     {
  135.     printf ("Can't open iec.library\n");
  136.     return;
  137.     }
  138.  
  139.     if (rda!=0) FreeArgs(rda);
  140.  
  141. }